CLING as a kernel

Recently a ROOT kernel was developed for jupyter notebooks. This means you can now drive cling from a notebook. This means you can now use ROOT and do all the things you are used to from other jupyter kernels.

Caveat

However because c++ is not meant to be a interactively programmed language, you can get weird errors if you re-execute a cell that defines a variable.

Remember that your input has to be c++!


In [ ]:
2. + 2

In [ ]:
TH1F h = TH1F("h", "A Histogram", 100,-10,10);

In [ ]:
h.FillRandom("gaus", 1000);

In [ ]:
h.GetMean();

In [ ]:
TCanvas c;
h.Draw();
// Save the histogram as a png
c.SaveAs("hist.png")

In [ ]:
// or display it in the notebook
c.Draw()

More complicated things

You can also do more complicated things like defining a new class.


In [ ]:
.cpp -d
class Rectangle {
    private:
        double w;
        double h;

    public:
    
        Rectangle(double w_, double h_) {
            w = w_;
            h = h_;
        }
        double area(void) {
            return w * h;
        }
        double perimiter(void) {
            return 2 * (w + h);
        }
};

In [ ]:
Rectangle r = Rectangle(5, 4);
r.area();

In [ ]:
// best way to get access to the value returned from r.area()
std::cout << "The area of the rectangle is: " << r.area() << std::endl;

In [ ]: